<template>
  <van-nav-bar title="确认订单" left-text="" left-arrow @click-left="goBack" />
  <!-- 没有地址显示 -->
  <van-contact-card
    v-if="JSON.stringify(addressinfo) == '{}'"
    type="add"
    @click="ChangeAddress"
  />
  <!-- 显示地址 -->
  <van-contact-card
    v-else
    type="edit"
    :tel="addressinfo.mobile"
    :name="addressinfo.consignee"
    @click="ChangeAddress"
  />
  <!-- 确认订单商品 -->
  <div v-if="ids != 0">
    <van-card
      v-for="item in list"
      :key="item.id"
      :price="item.total"
      :title="item.product.name"
      class="goods-card"
      :thumb="item.product.thumbs_text"
      :num="item.nums"
      @click="toProductInfo(item.proid)"
    >
      <template #desc>
        <div style="color: #969799">库存：{{ item.product.stock }}</div>
        <div>单价：{{ item.price }}</div>
      </template>
    </van-card>
  </div>
  <!-- 从商品详情确认订单 -->
  <div v-else>
    <van-card
      :price="productinfo.price"
      :title="productinfo.name"
      class="goods-card"
      :thumb="productinfo.thumbs_text"
      num="1"
      @click="toProductInfo(productinfo.id)"
    >
      <template #desc>
        <div style="color: #969799">库存：{{ productinfo.stock }}</div>
        <div>单价：{{ productinfo.price }}</div>
      </template>
    </van-card>
  </div>

  <van-cell-group inset>
    <van-field
      v-model="remark"
      rows="3"
      autosize
      label="订单备注"
      type="textarea"
      maxlength="120"
      placeholder="请输入订单备注"
      show-word-limit
    />
  </van-cell-group>
  <van-submit-bar :price="total" button-text="提交订单" @submit="onSubmit" />
</template>
<script setup>
const { cookies } = useCookies();
const route = useRoute();
const router = useRouter();
const ids = ref(0);
const LoginUser = reactive(cookies.get("LoginUser"));
let addressinfo = ref({});
const list = ref([]);
const remark = ref("");
const proid = ref(0);
let productinfo = ref({});
onMounted(() => {
  proid.value = route.query.proid ? route.query.proid : 0;
  ids.value = route.query.ids ? route.query.ids : 0;
  addressinfo.value = cookies.get("addressinfo") ? cookies.get("addressinfo") : {};
  // 判断是否有存储收货地址信息
  if (JSON.stringify(addressinfo.value) == "{}") {
    getAddressData();
  }
  // 判断是否从购物车跳转过来
  if (ids.value != 0) {
    getCartData();
  }

  if (proid.value != 0) {
    getConfirmData();
  }
});

const total = computed(() => {
  let num = 0;
  if (ids.value != 0) {
    list.value.forEach((item) => {
      num += parseFloat(item.total) * 100;
    });
  } else {
    num = parseFloat(productinfo.value.price) * 100;
  }

  return num;
});
const goBack = () => {
  router.go(-1);
};

const getCartData = async () => {
  let data = {
    busid: LoginUser.id,
    ids: ids.value,
  };
  let result = await Api.cartIndex(data);
  if (result.code == 1) {
    list.value = result.data;
  }
};

const getAddressData = async () => {
  let result = await Api.addressgetCheck({ busid: LoginUser.id });
  if (result.code == 1) {
    addressinfo.value = result.data;
  }
};

const ChangeAddress = () => {
  router.push({
    path: "/business/address/index",
    query: {
      action: "order",
    },
  });
};

const toProductInfo = (id) => {
  router.push({
    path: "/product/product/info",
    query: {
      proid: id,
    },
  });
};

const getConfirmData = async () => {
  let data = {
    proid: proid.value,
  };
  let result = await Api.productInfo(data);
  if (result.code == 1) {
    productinfo.value = result.data.productinfo;
  }
};

const onSubmit = async () => {
  let data = {
    busid: LoginUser.id,
    businessaddrid: addressinfo.value.id,
    amount: total.value / 100,
    remark: remark.value,
  };

  let result = "";
  if (ids.value != 0) {
    data.cartids = ids.value;
    // 判断从购物车确认订单或者是从商品详情
    result = await Api.orderAdd(data);
  } else {
    data.proid = proid.value;
    // 判断从购物车确认订单或者是从商品详情
    result = await Api.orderBuynow(data);
  }

  if (result.code == 1) {
    showNotify({
      type: "success",
      message: result.msg,
      onClose: () => {
        router.push("/business/order/index");
      },
    });
  } else {
    showNotify({ type: "danger", message: result.msg });
  }
};
</script>
